package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api("C端-地址簿接口相关接口")
@Slf4j
public class AddressBookController {

    private final AddressBookService bookService;

    @Autowired
    public AddressBookController(AddressBookService bookService) {
        this.bookService = bookService;
    }

    /**
     * 新增地址
     *
     * @param addressBook 地址body
     * @return 提示
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result<String> save(@RequestBody AddressBook addressBook) {
        log.info("用户新增地址:{}", addressBook);
        bookService.save(addressBook);
        return Result.success(MessageConstant.ADD_OK);
    }


    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return 当前登录用户的所有地址信息
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> getAddressBookList() {
        log.info("用户查询了地址");
        return Result.success(bookService.list(AddressBook.builder().userId(ThreadLocalUtil.get()).build()), MessageConstant.SELECT_OK);
    }

    /**
     * 查询默认地址
     *
     * @return 用户的默认地址
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getAddressDefault() {
        log.info("用户查询默认地址");
        List<AddressBook> addressBooks = bookService.list(AddressBook.builder().userId(ThreadLocalUtil.get()).isDefault(1).build());
        if (addressBooks != null && addressBooks.size() == 1) {
            return Result.success(addressBooks.get(0), MessageConstant.SELECT_OK);
        }
        return Result.error(MessageConstant.NO_DEFAULT_ADDRESS);
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook body
     * @return 提示
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        log.info("用户根据id修改地址：{}", addressBook);
        bookService.updateAddressBook(addressBook);
        return Result.success(MessageConstant.UPDATE_OK);
    }


    /**
     * 根据id删除地址
     *
     * @param id 地址ID
     * @return 提示
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<String> deleteAddressBook(Long id) {
        log.info("用户根据id删除地址:{}", id);
        bookService.deleteAddressBook(id);
        return Result.success(MessageConstant.OPERATE_OK);
    }


    /**
     * 根据id查询地址
     *
     * @param id 地址ID
     * @return 地址实体
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> findById(@PathVariable Long id) {
        log.info("用户根据id查询地址:{}", id);
        return Result.success(bookService.findById(id), MessageConstant.SELECT_OK);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook body
     * @return 提示
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result<String> setDefault(@RequestBody AddressBook addressBook) {
        log.info("用户设置默认地址:{}", addressBook);
        bookService.setDefault(addressBook);
        return Result.success(MessageConstant.OPERATE_OK);
    }
}
